PImage trung,ga,ngan,vit,chatthai,ro,background;

int ex,bx,y=100;
int r,e;
int savedTime;
int totalTime =5000;
int score=0,tscr=0,rem,cnt,ch=3;
int spd=3;
PFont mono;
float g=0,v=100,n=200;
float sg=2,sv=2,sn=2;

PImage lf1,lf2,lf3;


void setup()
{
    size(800, 600); 
    mono = createFont("lucon.ttf", 50);
    textFont(mono);  
    background=loadImage("background.png");
    trung=loadImage("trung.png");
    ga=loadImage("ga.png");
    vit=loadImage("vit.png");
    ngan=loadImage("ngan.png");
    chatthai=loadImage("chatthai.png");
    ro=loadImage("ro.png");
    lf1= loadImage("lf1.png"); 
    lf2= loadImage("lf2.png");   
    lf3= loadImage("lf3.png");   
    savedTime =millis();    
}
void draw()
{
  // chỉnh tốc độ khung hình
    frameRate(30);
  // cài đặt hình nền
    image(background,0,0);
    image(ro,bx,550);
  // line
    line (0,150,width,150);   
    line (0,550,width,550); 
   // Life 
  switch(ch)
   {
      case 3: 
             image(lf3,520,7);
             break;
      case 2: 
             image(lf2,520,7);
             break;
      case 1:
              image(lf1,520,7);
              break;
   }
 
  //
 int passedTime = millis()-savedTime;
 if (passedTime > totalTime)
 {
   r=(int)random(1,4);
    /* e=(int)random(1,4); */
   savedTime = millis();//save the rurrent time to restart
 }
  if (r==1)
 {
   ex =100;
   image(trung,ex,y);
 }
  else if(r==2)  
 {
    ex =300;
   image(trung,ex,y);
 }
  else if(r==3)  
 {
    ex =500;
    image(trung,ex,y);
  }
  else if(r==4)  
 {
     ex =700;
     image(trung,ex,y);
  }
/*
 if (e==1)
 {
   ex =100;
   image(chatthai,ex,y);
 }
  else if(e==2)  
 {
    ex =300;
   image(chatthai,ex,y);
 }
 else if(e==3)  
 {
    ex =500;
   image(chatthai,ex,y);
 }
 else if(e==4)  
 {
    ex =700;
   image(chatthai,ex,y);
 } 
   */
 // di chuyen trung
 y=y+spd;
 if(y> height-70)
 {
   y=100;
 }
   
    
 
  // di chuyen gio
  if(mousePressed)
  {
    if(mouseX<= 200)
    { 
      bx=80;
      image(ro,bx,550);
    }
     else if(mouseX>=201 && mouseX<=400)
     {
       bx=280;
       image(ro,bx,550);
     }
       else if(mouseX>=401 && mouseX<=600)
     {
       bx=480;
       image(ro,bx,550);
     }
        else if(mouseX>=601 && mouseX<=800)
     {
       bx=680;
       image(ro,bx,550);
     }    
  }

// gia cam di chuyen tren day
if (g<0 || g >height) 
  sg=-sg;
  g+=sg;
  image(ga,g,100);
  
if (v<0 || v >height)
  sv=-sv;
  v+=sv;
  image(vit,v,100);

if (n<0 || n >height)
  sn=-sn;
  n+=sn;
  image(ngan,n,100);
//tính điểm
int score=0;
if(bx <ex && (ex+40)<(bx+75) && (y+75)==562 && mousePressed)
{
 // printf(core);
}
else if( bx + 20 != ex && (y+75)==562)
{
  ch=ch-1;
  //printf("life is="ch);
}
if(ch==0)
{
  exit();
}
    text("score:" + score ,20,50);
}

